<template>
	<view>
		<view class="container">
			<uni-nav-bar left-icon="back" left-text="返回" title="我的下载" @clickLeft="goBack" color="white"
				backgroundColor="rgb(55, 138, 241)"></uni-nav-bar>
			<view class="body">
				<view class="goods" v-for="(item,index) in dataList" :key="index" @click="download(item.id)">
					<image src="../../static/pdficon.jpeg" mode="aspectFit"></image>
					<view class="content">
						<text>{{item.name}}</text>
						<view class="content1">
							<text class="price">￥{{item.price}}</text>
							<text class="time">{{item.time}}</text>
						</view>
					</view>
				</view>
			</view>
		</view>
	</view>
</template>

<script>
	export default {
		components: {},
		data() {
			return {
				dataList: []
			}
		},
    onLoad() {
      let that = this;
      this.$axios.get('/order/search',{
        headers: {
          token: localStorage.getItem('token')
        }
      }).then(res => {
        that.dataList = res.data.data.data;
      })
      console.log(this.dataList)
    },
		methods: {
			onClick(e) {
				console.log(e)
			},
			actionsClick(text) {
				uni.showToast({
					title: text,
					icon: 'none'
				})
			},
			goBack() {
				uni.navigateBack();
			},
      download(id){
        let url = localStorage.getItem('remote') +
            '/file/downloadFile2?userId='+localStorage.getItem('userId')+'&goodId='+id;
        const iframe = document.createElement("iframe");
        iframe.setAttribute("hidden","hidden");
        document.body.appendChild(iframe);
        iframe.onload = () => {
          if(iframe){
            iframe.setAttribute('src','about:blank');
          }
        };
        iframe.setAttribute("src",url);
        //document.body.removeChild(iframe);
      },
      download_(id) {
        let that = this;
        this.$axios.get('/file/downloadFile',{
          params: {
            goodId: id
          },
          headers: {
            token: localStorage.getItem('token')
          }
        }).then(res=> {
          console.log(res)
          let ext = 'doc';
          let blob = new Blob([res.data],{type:'application/actet-stream;charset=utf-8'});
          if (window.navigator.msSaveBlob) {
            try {
              // ie浏览器自带下载文件的方法
              window.navigator.msSaveBlob(res.data, 'a.doc');
            } catch (e) {
              console.log(e);
            }
          } else {
            let elink = document.createElement("a");
            elink.download = 'text' + "." + ext;
            elink.style.display = "none";
            let href = window.URL.createObjectURL(blob);
            elink.href = href;
            document.body.appendChild(elink);
            elink.click();
            document.body.removeChild(elink);
            window.URL.revokeObjectURL(href); // 释放掉blob对象
          }
        })
      }
		}
	}
</script>

<style>
	.container {
		padding: 0px;
		font-size: 14px;
		line-height: 24px;
	}

	.body {
		background: #CFCFCF;
		overflow: hidden;

	}

	.goods {
		display: flex;
		width: 100%;
		height: 120px;
		background: #fff;
		margin: 7px 0;
	}

	uni-image {
		width: 130px;
		height: 85px;
		margin-top: 17px;
	}

	text {
		font-size: 14px;
		color: #101010;

	}

	.content {
		margin-top: 10px;
		margin-right: 46px;

	}

	.content1 {
		margin-top: 43px;
	}

	.price {
		color: #ff0000;
		font-size: 16px;
		margin-right: 65px;
		font-weight: 700;
	}

	.time {
		color: #9FA1A3;
		font-size: 12px;
	}
</style>
